package com.hgk.common.common.ui.activity;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.common.dongqiuhui.apps.comm.net.BallAsyncTask;
import com.common.dongqiuhui.apps.comm.util.DataResult;
import com.common.handmark.pulltorefresh.library.PullToRefreshBase;
import com.common.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.common.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.common.handmark.pulltorefresh.library.PullToRefreshListView;
import com.common.handmark.pulltorefresh.library.internal.BallBaseAdapter;

import java.util.List;

import width.hgk.wanwan.R;

/**
 * 涓婃媺涓嬫媺鍒锋柊妗嗘灦(甯︽媿鐓у姛鑳�)
 * 
 * @author wangbingqi
 * 
 */
public abstract class PhotoUpAndDownRefreshActivity<T> extends
		BallPhotoToolActivity implements OnRefreshListener2<ListView>,
		OnItemClickListener, OnClickListener {

	private PullToRefreshListView mPullRefreshListView;

	public AsyncTask<String, Integer, DataResult> dataAsyncTask;

	public BallBaseAdapter<T> mBaseAdapter;

	/**
	 * 鏄惁鍔犺浇loading
	 */
	private boolean isFirstLoading = true;
    /**
     * 甯栧瓙涓嶅瓨鍦�
     */
	public boolean isNonExist=false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
		mPullRefreshListView.setMode(onCreate());
		mPullRefreshListView.setShowIndicator(false);
		initAdapter(getAdapte());
		getTopicReadHistory();
		onCreateEnd();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	/**
	 * 鍒濆鍖� adapter
	 * 
	 * @param adapter
	 */
	private void initAdapter(BallBaseAdapter<T> adapter) {
		this.mBaseAdapter = adapter;
		mPullRefreshListView.setAdapter(mBaseAdapter);
		mPullRefreshListView.setOnRefreshListener(this);
		mPullRefreshListView.setOnItemClickListener(this);
	}

	/**
	 * 璁剧疆item棰滆壊
	 * 
	 * @param imgId
	 */
	protected final void setDivider(int imgId) {
		if (imgId == 0) {
			mPullRefreshListView.getRefreshableView().setDivider(null);
		} else {
			mPullRefreshListView.getRefreshableView().setDivider(
					this.getResources().getDrawable(imgId));
		}
	}

	/**
	 * 璁剧疆item涔嬮棿鐨凞ividerHeight
	 * 
	 * @param height
	 */
	protected final void setDividerHeight(int height) {
		mPullRefreshListView.getRefreshableView().setDividerHeight(height);
	}

	/**
	 * 璁剧疆鏄惁鏄涓�娆″姞杞�
	 * 
	 * @param isb
	 */
	protected final void setFirsLoading(boolean isb) {
		this.isFirstLoading = isb;
	}

	/**
	 * 璁剧疆鏄惁鏄涓�娆″姞杞�
	 */
	protected final boolean isFirsLoading() {
		return isFirstLoading;
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		if (!isFirstLoading) {
			onDownRefresh();
		}
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
		if (!isFirstLoading) {
			onUpRefresh();
		}
	}

	/**
	 * 璁剧疆閫傞厤鍣�
	 * 
	 * @return
	 */
	protected abstract BallBaseAdapter<T> getAdapte();

	/**
	 * 鍒濆鍖�
	 * 
	 */
	protected abstract Mode onCreate();

	/**
	 * 鍔犺浇缁撴潫
	 */
	protected void onCreateEnd() {
		onNetStart();
	};

	/**
	 * 涓嬫媺鍒锋柊浜嬩欢
	 */
	protected abstract void onDownRefresh();

	/**
	 * 涓婃媺鍒锋柊鏃堕棿
	 */
	protected abstract void onUpRefresh();

	/**
	 * 鍔犺浇鏁版嵁
	 */
	protected final void onNetStart() {
		//dataAsyncTask = new DownRefreshAsyncTask(this);
		//dataAsyncTask.execute();
	}

	/**
	 * 鍒锋柊鏁版嵁
	 */
	protected final void onRefresh() {
		hideLoadingView();
		mPullRefreshListView.setDataLoadingState(false);
		mBaseAdapter.notifyDataSetChanged();
		mPullRefreshListView.onRefreshComplete();
	}

	/**
	 * 鍔犺浇缁撴潫鍏抽棴鍒锋柊鍔ㄧ敾
	 */
	protected final void onEndRefresh() {
		mPullRefreshListView.setDataLoadingState(false);
		hideLoadingView();
		mPullRefreshListView.onRefreshComplete();
	}

	/**
	 * 娓呴櫎鏁版嵁
	 */
	protected final void clearData() {
		mBaseAdapter.clear();
	}

	/**
	 * 娓呴櫎鏁版嵁
	 */
	protected final void notifyDataSetChanged() {
		mBaseAdapter.notifyDataSetChanged();
	}

	/**
	 * 璁剧疆鏁版嵁
	 * 
	 * @param data
	 */
	protected final void setData(List<T> data) {
		mBaseAdapter.setData(data);
	}

	/**
	 * 鑱旂綉鎴愬姛
	 * 
	 * @param result
	 */
	protected abstract void success(DataResult result);

	/**
	 * 鑾峰彇鑱旂綉鏁版嵁
	 * 
	 * @return
	 */
	protected abstract DataResult getDataResult();

	/**
	 * 鑱旂綉澶辫触
	 * 
	 * @param result
	 */
	protected void failure(DataResult result) {
//		hideLoadingView();
//		isNonExist=true;
//		if (BabytreeController.JSONExceptionCode == result.status) {
//			showNetView();
//			setNetdata(getResources().getString(R.string.dataerror), null);
//		} else if (BabytreeController.NetworkExceptionCode == result.status) {
//			if (isFirstLoading) {
//				showNetView();
//			} else {
//				Toast.makeText(mContext,
//						getResources().getString(R.string.network_error),
//						Toast.LENGTH_SHORT).show();
//			}
//		} else {
//			if (TextUtils.isEmpty(result.message)) {
//				result.message = getResources().getString(R.string.dataerror);
//			}
//			showNoDataView();
//			setNodata(result.message, null);
//
//		}
		onEndRefresh();
	}

	/**
	 * 璁剧疆鑱旂綉璇锋眰鏂囧瓧
	 * 
	 * @return
	 */
	protected String getDialogMessage() {
		return "";
	}

	/**
	 * 鍒楄〃
	 * 
	 * @author wangbingqi
	 * 
	 */
	final class DownRefreshAsyncTask extends BallAsyncTask {

		public DownRefreshAsyncTask(Context context) {
			super(context);
			// 绗竴娆″姞杞借缃�
			if (isFirstLoading) {
				showLoadingView();
			}
		}

		@Override
		protected DataResult toNet(String[] params) {
			return PhotoUpAndDownRefreshActivity.this.getDataResult();
		}

		@Override
		protected void success(DataResult result) {
			hideNetView();
			hideNodataView();
			hideLoadingView();
			PhotoUpAndDownRefreshActivity.this.success(result);
			isFirstLoading = false;
		}

		@Override
		protected void failure(DataResult result) {
			mPullRefreshListView.setLoadMore();
			PhotoUpAndDownRefreshActivity.this.failure(result);
		}

		@Override
		protected String getDialogMessage() {
			return PhotoUpAndDownRefreshActivity.this.getDialogMessage();
		}

	}

	@Override
	public int getBodyView() {
		return R.layout.playing_list_view;
	}

	/**
	 * 鑾峰彇PullToRefreshListView
	 * 
	 * @return
	 */
	public PullToRefreshListView getPullRefreshListView() {
		return mPullRefreshListView;
	}

	/**
	 * 婊氬姩鍒版煇涓�position
	 * 
	 * @param position
	 */
	protected void toSelectPosition(int position) {
		if (mBaseAdapter == null) {
			return;
		}
		if (position > mBaseAdapter.getCount()) {
			return;
		}
		mPullRefreshListView.getRefreshableView().setSelection(position);
	}

	// 鏃犵綉缁滃埛鏂版寜閽簨浠�
	@Override
	public void noNetReflushBtn() {
		onNetStart();
	}

	// 璇讳笂娆￠槄璇荤殑璁板綍
	protected void getTopicReadHistory() {
	}
}
